function pp(url) {
	window.open(url);
}

function cc(url, params, method, fx) {
	if(typeof im_hide == "function") {
		im_hide();	
	}
	loadata('main', url, params, method, fx);
}

function loadata(target, url, params, method, fx) {
	new Ajax.Request(url, {method:(method? method : 'GET'), parameters:params, 
									onComplete:function(request) {
										var content = request.responseText;
										$(target).innerHTML = content;
										content.evalScripts();
										if(fx) fx(request);
									}
					});
}

var trackingHandlers = {
    	onCreate: function() {
        	$('loading').style.display = 'block';
        },
        onLoaded: function() {
            $('loading').style.display = 'none';
        }
};

Ajax.Responders.register(trackingHandlers);	

function submitForm(form, params, method, fx) {
	cc(form.getAttribute('action').appendURL(Form.serialize(form)), params, method, fx);
}

function submitFormValidation(form, params, method, fx) {
	new Ajax.Request(form.getAttribute('validator').appendURL(Form.serialize(form)), 
						{onSuccess:function(request) {
							var errorMessage = request.responseText.evalJSON();
							if(errorMessage.type.name == "success") {
								submitForm(form, params, method);
							}
							else {
								$('error').show();
								$('error').innerHTML = errorMessage.message;
							}
							if(fx) fx(errorMessage.type.name);
						}
					});
}

function submitFormValidation0(form, fx) {
	new Ajax.Request(form.getAttribute('validator').appendURL(Form.serialize(form)), 
						{onSuccess:function(request) {
							var errorMessage = request.responseText.evalJSON();
							if(errorMessage.type.name == "success") {
								fx(form);
							}
							else {
								$('error').show();
								$('error').innerHTML = errorMessage.message;
							}
						}
					});
}

function popupSubmitForm(form, params, method, fx) {
	pp(form.getAttribute('action').appendURL(Form.serialize(form)), params, method, fx);
}

function popupSubmitFormValidation(form, params, method, fx) {
	new Ajax.Request(form.getAttribute('validator').appendURL(Form.serialize(form)), 
						{onSuccess:function(request) {
							var errorMessage = request.responseText.evalJSON();
							if(errorMessage.type.name == "success") {
								popupSubmitForm(form, params, method);
							}
							else {
								$('error').show();
								$('error').innerHTML = errorMessage.message;
							}
							if(fx) fx(errorMessage.type.name);
						}
					});
}

function requestSelectOption(url, source, target) {			
	$(target).options.length = 0;
	if($(source)) {
		if(!$(source).value) {
			return;
		}
		url = url.appendURL($(source).name, $(source).value);
	}
	new Ajax.Request(url, {method:'GET', 
							onComplete:function(request) {
								var result = request.responseText.evalJSON();
								$A(result).each(function(line) {
									var option = document.createElement('option');
									option.setAttribute('value', line.value);
									option.appendChild(document.createTextNode(line.caption));
									$(target).appendChild(option);
								});
							}
			});
}

function selectAllOptionsExceptSelection(selectId) {
	var result = new Array();
	$A($(selectId).options).each(function(option) {
		if(!option.selected)
			result.push($(selectId).name + '=' + option.value);
	});
	return result.join('&');
}

function requestTooltipInfo(event, url) {
	showTooltip(event, '...');
	new Ajax.Request(url, {onComplete:function(request) {
	                 			$('dhtmlgoodies_tooltip').innerHTML = request.responseText;
	                       }
	                 	}
	                 )
	
}
   
/* Implementation */

Object.extend(String.prototype, {
	appendURL: function(pair, value) {
		var param = pair;
		if(value) param = encodeURIComponent(pair)+ '=' + encodeURIComponent(value);
		
		if(this.indexOf('?') == -1)	{
			return this + '?' + param;
		}
		if(this.lastIndexOf('?') == this.length - 1) {
			return this + param;
		}
		if(this.lastIndexOf('&') == this.length - 1) {
			return this + param;
		}
		return this + '&' + param;
	}
});

function round(n, x) {
	var y = Math.floor(Math.pow(10, x));
	return Math.floor((n * 1.0 / y) + 0.5) * y;
}

